package com.lxj.xpopupdemo;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class TestActivity extends AppCompatActivity  {
    WebView webView;
    TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tt);
        webView=findViewById(R.id.webView);
        textView=findViewById(R.id.txt);
        initWebView();
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  sendBroadcast(new Intent("showDialog"));
                finish();
//                duqu();

            }
        });
        webView.loadUrl("file:///android_asset/h5/index.html");

    }

    private void duqu(){
        new Thread(){
            public void run() {
                try {
                    Thread.sleep(10000);
                    Process process = Runtime.getRuntime().exec("logcat -b radio");
                    InputStream is = process.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str ;
                    final StringBuilder stringBuilder=new StringBuilder();
                    while(null != (str = br.readLine())){
                        LogUtils.i("MyService", str);
                        stringBuilder.append(str).append("\n");
                    }
                    final String ttt=str;
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(stringBuilder.toString());
                        }
                    },100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
        }.start();




    }






    /**
     * 请求读写权限
     */
    private void checkReadPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            TelephonyManager tm = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
            Log.e(TestActivity.class.getSimpleName(),"tid="+tm.getDeviceId());


        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)){
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                        123);
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                        123);
            }
        }
    }

    private void initWebView() {
        //声明WebSettings子类
        WebSettings webSettings = webView.getSettings();

//插件支持设置
        webSettings.setJavaScriptEnabled(true); // 是否支持Javascript，默认false
//        webSettings.setPluginsEnabled(true); //是否支持插件
        webSettings.setSupportMultipleWindows(false);// 是否支持多窗口，默认false
        webSettings.setJavaScriptCanOpenWindowsAutomatically(false);// 是否可用Javascript(window.open)打开窗口，默认false

//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作设置
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//资源加载设置
        webSettings.setLoadsImagesAutomatically(true); // 是否自动加载图片
        webSettings.setBlockNetworkImage(false); // 禁止加载网络图片
        webSettings.setBlockNetworkLoads(false); // 禁止加载所有网络资源

//资源访问设置
        webSettings.setAllowContentAccess(true); //是否可访问Content Provider的资源，默认 true
        webSettings.setAllowFileAccess(true);  //是否可访问本地文件，默认 true
        webSettings.setAllowFileAccessFromFileURLs(false); //是否允许通过file url加载的Javascript读取本地文件，默认 false
        webSettings.setAllowUniversalAccessFromFileURLs(false);//是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https)，默认 false

//存储缓存设置
        webSettings.setDomStorageEnabled(true); //是否启用 DOM storage API，默认 false
        webSettings.setDatabaseEnabled(true);  //是否启用 Database storage API，默认 false
        webSettings.setAppCacheEnabled(true); //是否启用缓存
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //缓存模式设置

//默认文本设置
        webSettings.setDefaultFontSize(16);// 默认文字尺寸，默认值16，取值范围1-72
        webSettings.setDefaultFixedFontSize(16);// 默认等宽字体尺寸，默认值16
        webSettings.setMinimumFontSize(8);// 最小文字尺寸，默认值 8
        webSettings.setMinimumLogicalFontSize(8); // 最小文字逻辑尺寸，默认值 8
        webSettings.setTextZoom(100); // 文字缩放百分比，默认值 100

        webView.setWebChromeClient(new WebChromeClient(){
        });
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        DefaultWebClient mDefaultWebClient = DefaultWebClient
                .createBuilder()
                .setActivity(this)
                .setWebClientHelper(true)
                .setWebView(webView)
                .setInterceptUnkownUrl(false)
                .setUrlHandleWays(DefaultWebClient.DERECT_OPEN_OTHER_PAGE)
                .build();
        webView.setWebViewClient(mDefaultWebClient);

    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        if (webView.canGoBack()){
            webView.goBack();
        }

    }
}
